package com.ruoyi.control.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.control.dto.Mainventilator1HistoryDto;
import com.ruoyi.control.dto.Mainventilator2HistoryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.control.mapper.Mainventilator2HistoryMapper;
import com.ruoyi.control.domain.Mainventilator2History;
import com.ruoyi.control.service.IMainventilator2HistoryService;

/**
 * 主通风机2号风机Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-08
 */
@Service
public class Mainventilator2HistoryServiceImpl extends ServiceImpl<Mainventilator2HistoryMapper, Mainventilator2History> implements IMainventilator2HistoryService //修改
{
    @Autowired
    private Mainventilator2HistoryMapper mainventilator2HistoryMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询主通风机2号风机
     *
     * @param id 主通风机2号风机主键
     * @return 主通风机2号风机
     */
    @Override
    public Mainventilator2History selectMainventilator2HistoryById(String id) {
        return mainventilator2HistoryMapper.selectMainventilator2HistoryById(id);
    }

    /**
     * 查询主通风机2号风机列表
     *
     * @param mainventilator2History 主通风机2号风机
     * @return 主通风机2号风机
     */
    @Override
    public List<Mainventilator2History> selectMainventilator2HistoryList(Mainventilator2History mainventilator2History) {
        return mainventilator2HistoryMapper.selectMainventilator2HistoryList(mainventilator2History);
    }

    /**
     * 新增主通风机2号风机
     *
     * @param mainventilator2History 主通风机2号风机
     * @return 结果
     */
    @Override
    public int insertMainventilator2History(Mainventilator2History mainventilator2History) {
        return mainventilator2HistoryMapper.insertMainventilator2History(mainventilator2History);
    }

    /**
     * 修改主通风机2号风机
     *
     * @param mainventilator2History 主通风机2号风机
     * @return 结果
     */
    @Override
    public int updateMainventilator2History(Mainventilator2History mainventilator2History) {
        mainventilator2History.setUpdateTime(DateUtils.getNowDate());
        return mainventilator2HistoryMapper.updateMainventilator2History(mainventilator2History);
    }

    /**
     * 批量删除主通风机2号风机
     *
     * @param ids 需要删除的主通风机2号风机主键
     * @return 结果
     */
    @Override
    public int deleteMainventilator2HistoryByIds(String[] ids) {
        return mainventilator2HistoryMapper.deleteMainventilator2HistoryByIds(ids);
    }

    /**
     * 删除主通风机2号风机信息
     *
     * @param id 主通风机2号风机主键
     * @return 结果
     */
    @Override
    public int deleteMainventilator2HistoryById(String id) {
        return mainventilator2HistoryMapper.deleteMainventilator2HistoryById(id);
    }

    @Override
    public Integer[] selectMainventilator2HistoryByTagName(String tagName) {
        Integer[] history = new Integer[60 * 24];
        List<Mainventilator2History> historyList = mainventilator2HistoryMapper.selectMainventilator2HistoryByTagName(tagName);
        for (int i = 0; i < 60 * 24; i++) {
            if (i >= historyList.size()) {
                history[i] = 0;
            } else {
                double doubleValue = Double.parseDouble(historyList.get(i).getValue());
                int intValue = (int) doubleValue; // 如果需要四舍五入，可以使用 Math.round(doubleValue)
                history[i] = intValue;
            }
        }
        return history;
    }


    @Override
    public int getMainTwoTotal() {
        final String key = "tfTwoKey";
        int total = 0;
        String cachedValue = redisCache.getCacheObject(key);

        List<Mainventilator2HistoryDto> list = mainventilator2HistoryMapper.getMainTwoTotal();
        total = list.size();
        redisCache.setCacheObject(key, String.valueOf(total));

//        if (cachedValue == null || cachedValue.isEmpty()) {
//            List<Mainventilator2HistoryDto> list = mainventilator2HistoryMapper.getMainTwoTotal();
//            total = list.size();
//            redisCache.setCacheObject(key, String.valueOf(total));
//        } else {
//            total = Integer.parseInt(cachedValue);
//            System.out.println("读取tfTwoKey缓存: " + total);
//        }
        return Integer.parseInt(cachedValue);
    }
}
